import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";

/**
 * 创建 可 拖动的 字段
 * @date 2019/1/4
 */
import React from 'react';
import { DragSource } from 'react-dnd';
import { DragType, emptyFieldType } from "../../config";
import createDropField from "../Drop/createDropField";
import styles from "../index.less?modules";
export default function createDrawDragField() {
  var _dec, _class;

  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref$dragType = _ref.dragType,
      dragType = _ref$dragType === void 0 ? DragType.drawDragField : _ref$dragType,
      _ref$canDrag = _ref.canDrag,
      canDrag = _ref$canDrag === void 0 ? defaultCanDrop : _ref$canDrag,
      _ref$beginDrag = _ref.beginDrag,
      beginDrag = _ref$beginDrag === void 0 ? defaultBeginDrag : _ref$beginDrag,
      _ref$dropField = _ref.dropField,
      dropField = _ref$dropField === void 0 ? createDropField() : _ref$dropField;

  var drawDragFieldSpec = {
    beginDrag: beginDrag,
    canDrag: canDrag
  };

  var drawDragFieldCollect = function drawDragFieldCollect(connect, monitor) {
    var connectDragSource = connect.dragSource();
    var isDragging = monitor.isDragging();
    return {
      connectDragSource: connectDragSource,
      isDragging: isDragging
    };
  };

  var DropField = dropField;
  var DrawDragField = (_dec = DragSource(dragType, drawDragFieldSpec, drawDragFieldCollect), _dec(_class = /*#__PURE__*/function (_React$Component) {
    _inherits(DrawDragField, _React$Component);

    var _super = _createSuper(DrawDragField);

    function DrawDragField(props) {
      var _this;

      _classCallCheck(this, DrawDragField);

      _this = _super.call(this, props);
      _this.state = {};
      return _this;
    }

    _createClass(DrawDragField, [{
      key: "render",
      value: function render() {
        var _this$props = this.props,
            connectDragSource = _this$props.connectDragSource,
            isDragging = _this$props.isDragging,
            fieldOptions = _this$props.fieldOptions,
            currentEditField = _this$props.currentEditField,
            _this$props$component = _this$props.component,
            component = _this$props$component === void 0 ? {} : _this$props$component,
            pComponent = _this$props.pComponent,
            children = _this$props.children,
            onSwapField = _this$props.onSwapField,
            onAddField = _this$props.onAddField,
            onActiveField = _this$props.onActiveField;
        var drawDragFieldClassName = [styles['draw-drag-field']];
        return isDragging ? /*#__PURE__*/React.createElement("div", {
          className: "".concat(styles['draw-drag-field'], " ").concat(styles['draw-drag-field-dragging'])
        }, /*#__PURE__*/React.createElement("div", {
          style: {
            visibility: 'hidden'
          }
        }, children)) : connectDragSource && connectDragSource( /*#__PURE__*/React.createElement("div", {
          className: drawDragFieldClassName.join(' ')
        }, /*#__PURE__*/React.createElement(DropField, {
          pComponent: pComponent,
          component: component,
          onSwapField: onSwapField,
          onAddField: onAddField,
          onActiveField: onActiveField,
          fieldOptions: fieldOptions,
          currentEditField: currentEditField
        }, children)));
      }
    }]);

    return DrawDragField;
  }(React.Component)) || _class);
  return DrawDragField;
}

function defaultCanDrop(props) {
  var component = props.component;
  return component.componentType !== emptyFieldType;
}

function defaultBeginDrag(props) {
  return {
    component: props.component,
    pComponent: props.pComponent,
    fieldOptions: props.fieldOptions
  };
}